//Daen
package cn.lanqiao.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.junit.Test;
import cn.lanqiao.dao.TeacherDao;
import cn.lanqiao.entiy.Teacher;

public class TeacherDaoImpl  implements TeacherDao{
	//获取全部教练信息数组
	@Test
	public Object[][] TeacherList() {
		Object[][] datas = null;
		//1、创建JsfUtil类并自动加载Oracle数据库驱动程序
 		cn.lanqiao.util.JsfUtil j=new cn.lanqiao.util.JsfUtil();	
 		//2、使用类里面的方法拿到得到连接
 		Connection conn =j.getConnection();
 		//3、需要一个向数据库发送SQL的一个命令对象:常见有三个(Statement--不能带参数 |PreparedStatement--可以带参数 |CallableStatement--调用过程与函数 )
 		String sql="select * from J_TEACHER";
 		//3、通过连接对象创建statement对象
 		PreparedStatement ps =null;
 		try {
			ps = conn.prepareStatement(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 		ResultSet rs = null;
		try {
			rs = ps.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int i=0;
		datas = new Object[TeacherNumber()][];

 		try {
			while(rs.next()) {
				String T_ID = rs.getString("T_ID");
				String TNAME = rs.getString("TNAME");
				String TSEX = rs.getString("TSEX");
				String P_ID = rs.getString("P_ID");
				String M_ID = rs.getString("M_ID");
				String TPHONE = rs.getString("TPHONE");
//				System.out.println(T_ID+"-" + TNAME+"-"+TSEX+"-"+P_ID+"-"+M_ID+"-"+TPHONE);
				datas[i] = new Object[] {T_ID,TNAME,TSEX,P_ID,M_ID,TPHONE};
				i=i+1;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
 		//6、关闭对象
 		j.close(rs, ps, conn);
		return datas;
	}

	//获取教练人员数
	@Override
	
	public int TeacherNumber() {
		cn.lanqiao.util.JsfUtil j=new cn.lanqiao.util.JsfUtil();	
 		//2、使用类里面的方法拿到得到连接
 		Connection conn =j.getConnection();
 		//3、需要一个向数据库发送SQL的一个命令对象:常见有三个(Statement--不能带参数 |PreparedStatement--可以带参数 |CallableStatement--调用过程与函数 )
 		String sql="select * from J_TEACHER";
 		//3、通过连接对象创建statement对象
 		PreparedStatement ps =null;
 		try {
			ps = conn.prepareStatement(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 		ResultSet rs = null;
		try {
			rs = ps.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int i=0;
 		try {
			while(rs.next()) {

				i=i+1;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
 		//6、关闭对象
 		j.close(rs, ps, conn);
		return i;
		
	}

	//通过手机获取指定教练信息
	@Override
	public Teacher searchTeacher(String PHONE) {
		Teacher tc = null;
 		cn.lanqiao.util.JsfUtil j=new cn.lanqiao.util.JsfUtil();	
 		Connection conn =j.getConnection();
 		String sql="select * from J_TEACHER WHERE TPHONE='"+PHONE+"'";
 		PreparedStatement ps =null;
 		try {
			ps = conn.prepareStatement(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 		ResultSet rs = null;
		try {
			rs = ps.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 		try {
			while(rs.next()) {
				String T_ID = rs.getString("T_ID");
				String TNAME = rs.getString("TNAME");
				String TSEX = rs.getString("TSEX");
				String P_ID = rs.getString("P_ID");
				String M_ID = rs.getString("M_ID");
				String TPHONE = rs.getString("TPHONE");
				System.out.println(T_ID+"-" + TNAME+"-"+TSEX+"-"+P_ID+"-"+M_ID+"-"+TPHONE);
				tc = new Teacher(T_ID,TNAME,TSEX,P_ID,M_ID,TPHONE);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
 		//6、关闭对象
 		j.close(rs, ps, conn);
		return tc;
	}
	
	//通过教练ID获取指定教练信息
	@Override
	public Teacher searchTeacher(int ID) {
		Teacher tc = null;
 		cn.lanqiao.util.JsfUtil j=new cn.lanqiao.util.JsfUtil();	
 		Connection conn =j.getConnection();
 		String sql="select * from J_TEACHER WHERE T_ID='"+ID+"'";
 		PreparedStatement ps =null;
 		try {
			ps = conn.prepareStatement(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 		ResultSet rs = null;
		try {
			rs = ps.executeQuery();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
 		try {
			while(rs.next()) {
				String T_ID = rs.getString("T_ID");
				String TNAME = rs.getString("TNAME");
				String TSEX = rs.getString("TSEX");
				String P_ID = rs.getString("P_ID");
				String M_ID = rs.getString("M_ID");
				String TPHONE = rs.getString("TPHONE");
				System.out.println(T_ID+"-" + TNAME+"-"+TSEX+"-"+P_ID+"-"+M_ID+"-"+TPHONE);
				tc = new Teacher(T_ID,TNAME,TSEX,P_ID,M_ID,TPHONE);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
 		//6、关闭对象
 		j.close(rs, ps, conn);
		return tc;
	}

	
	

}
